package com.five.calendar.ui.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.five.calendar.R;
import com.five.calendar.utils.TimeUtils;

/**
 * Created by zhuxiaoxinsx on 17-9-4.
 */

public class TipsChooseLayout extends RelativeLayout {
    private RadioGroup mRadioGroup;
    private RadioButton mChooseItem;
    private OnRadioChange mCallBack;
    private TextView mCancelView;
    public TipsChooseLayout(Context context) {
        super(context);
    }

    public TipsChooseLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public TipsChooseLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setOnRadioChange(OnRadioChange callback){
        this.mCallBack = callback;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        initView();
    }

    private void initView() {
        mChooseItem = (RadioButton) findViewById(R.id.button9);
        mRadioGroup = (RadioGroup) findViewById(R.id.group);
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                mChooseItem = (RadioButton) findViewById(checkedId);
                if(mCallBack != null){
                    mCallBack.onChanged(mChooseItem.getText().toString(), getTimems(checkedId));
                }
                setVisibility(GONE);
            }
        });
        mCancelView = (TextView) findViewById(R.id.cancel);
        mCancelView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                setVisibility(GONE);
            }
        });
    }

    public interface OnRadioChange{
        void onChanged(String str, long times);
    }

    public long getTimems(int checkedId){
        switch (checkedId){
            case R.id.button1:
                return TimeUtils.MINUTE * 10;
            case R.id.button2:
                return TimeUtils.MINUTE * 15;
            case R.id.button3:
                return TimeUtils.MINUTE * 30;
            case R.id.button4:
                return TimeUtils.MINUTE * 60;
            case R.id.button5:
                return TimeUtils.HOUR * 2;
            case R.id.button6:
                return TimeUtils.DAY;
            case R.id.button7:
                return TimeUtils.DAY * 2;
            case R.id.button8:
                return TimeUtils.DAY * 7;
            case R.id.button9:
                return 0;
        }
        return 0;
    }

    public void checkRadioButton(int id){
        mRadioGroup.check(id);
    }
}
